package com.zaker.touchtv.modules.polling;

import android.app.job.JobInfo;
import android.app.job.JobScheduler;
import android.content.ComponentName;
import android.content.Context;
import android.os.Build;
import android.os.PersistableBundle;
import android.support.annotation.RequiresApi;
import android.util.Log;

import java.util.List;

import static com.zaker.touchtv.modules.polling.PollSingleSchedulerService.STAT_KEY;
import static com.zaker.touchtv.modules.polling.PollSingleSchedulerService.UID_KEY;
import static com.zaker.touchtv.modules.polling.PollSingleSchedulerService.USER_NAME_KEY;
import static com.zaker.touchtv.utils.Constants.LOG_TAG;

/**
 * Created by Zonda on 2016/10/28. 轮询帮助类
 */


public class PollSchedulerHelper {

  private final static long POLL_PERIODS = 5000;//5秒

  private final static int POLL_JOB_ID = 123;

  @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
  public static int startPollScheduler(Context packageContext) {

    Log.i(LOG_TAG, "startPollScheduler is run ");

    JobScheduler pollJobScheduler = (JobScheduler) packageContext
        .getSystemService(Context.JOB_SCHEDULER_SERVICE);

    if (hasPendingJob(pollJobScheduler, POLL_JOB_ID)) {
      return -1;
    }

    Log.i(LOG_TAG, "startPollScheduler is run -----");

    final PersistableBundle persistableBundle = new PersistableBundle();
    persistableBundle.putString(UID_KEY, "test_uid");
    persistableBundle.putString(USER_NAME_KEY, "test_name");
    persistableBundle.putInt(STAT_KEY, -1);

    final JobInfo pollJobInfo = new JobInfo.Builder(POLL_JOB_ID
        , new ComponentName(packageContext, PollSingleSchedulerService.class))
        .setExtras(persistableBundle)
        //.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
        .setBackoffCriteria(POLL_PERIODS, JobInfo.BACKOFF_POLICY_LINEAR)
        .setPersisted(true)
        .setRequiresCharging(false)
//        .setMinimumLatency(POLL_PERIODS)
       // .setRequiresDeviceIdle(true)
        .setPeriodic(POLL_PERIODS)
        .build();

    int result = pollJobScheduler.schedule(pollJobInfo);

    if (result == JobScheduler.RESULT_SUCCESS) {
      Log.d(LOG_TAG, "startPollScheduler Job scheduled successfully!");
    }

    return result;
  }

  @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
  public static void cancelPollScheduler(Context packageContext) {

    Log.i(LOG_TAG, "cancelPollScheduler is run ");

    JobScheduler pollJobScheduler = (JobScheduler) packageContext
        .getSystemService(Context.JOB_SCHEDULER_SERVICE);
    pollJobScheduler.cancel(POLL_JOB_ID);
  }

  @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
  private static boolean hasPendingJob(JobScheduler jobScheduler, int jobId) {

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
      JobInfo pendingJob = jobScheduler.getPendingJob(jobId);
      boolean isPendingNonNull = pendingJob != null;
      Log.i(LOG_TAG, "hasPendingJob isPendingNonNull: " + isPendingNonNull);
      if (isPendingNonNull) {
        Log.i(LOG_TAG, " pendingJob id : " + pendingJob.getId() + " jobId: " + jobId);
      }
    }

    List<JobInfo> allPendingJobInfos = jobScheduler.getAllPendingJobs();
    for (JobInfo pendingJobInfo : allPendingJobInfos) {

      Log.i(LOG_TAG, " pendingJobInfo id : " + pendingJobInfo.getId() + " jobId: " + jobId);

      if (pendingJobInfo.getId() == jobId) {
        return true;
      }
    }
    return false;
  }
}
